﻿using B.S.DataBasic.Domain.GoodInfra;
using B.S.DataBasic.ErrorCode;
using B.S.DataBasic.Write.API.Application;
using B.S.DataBasic.Write.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;


namespace B.S.DataBasic.Write.API.Controllers
{
    /// <summary>
    /// 溯源表
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TranceController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly IWebHostEnvironment _webHostEnvironment;
        public TranceController(IMediator mediator, IWebHostEnvironment webHostEnvironment)
        {
            this.mediator = mediator; 
            this._webHostEnvironment = webHostEnvironment;
        }

        /// <summary>
        /// 溯源表添加
        /// </summary>
        /// <param name="add"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<Traceabilitable>> AddTrance(AddTraceCommand add)
        {
            return await mediator.Send(add);
        }

        /// <summary>
        /// 溯源表删除
        /// </summary>
        /// <param name="add"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<Traceabilitable>> DelTrance(DelTranceCommand del)
        {
            return await mediator.Send(del);
        }

        ///// <summary>
        ///// 图片
        ///// </summary>
        ///// <param name="file"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public IActionResult Img(IFormFile file)
        //{
        //    try
        //    {
        //        //全球唯一标识符
        //        var str = Guid.NewGuid().ToString();
        //        //3.完成图片上传
        //        string p = Directory.GetCurrentDirectory() + @"\wwwroot\files\" + str + file.FileName;
        //        using (FileStream fs = new FileStream(p, FileMode.Create))
        //        {
        //            file.CopyTo(fs);
        //            fs.Flush();
        //        }
        //        return Ok("http://localhost:62301/Coverimage/" + str + file.FileName);

        //    }
        //    catch (Exception)
        //    {

        //        throw;
        //    }
        //}

        /// <summary>
        /// 上传图片 
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Upload(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("请选择要上传的文件");
            }

            var now = DateTime.Now;
            string yearFolder = now.Year.ToString();
            string monthFolder = now.Month.ToString("D2");
            string dayFolder = now.Day.ToString("D2");

            string uploadsRoot = Path.Combine(_webHostEnvironment.WebRootPath, "uploads");
            string uploadPath = Path.Combine(uploadsRoot, yearFolder, monthFolder, dayFolder);

            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }

            string filePath = Path.Combine(uploadPath, file.FileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                file.CopyTo(stream);
            }

            return Ok($"文件上传成功，保存路径：{filePath}");
        }


    }
}
